//-----------------------------------------------------------------------
// <copyright file="UnitPriceDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class UnitPriceDAOBase
    {
        #region Common methods
        public virtual UnitPrice CreateUnitPriceFromReader(IDataReader reader)
        {
            UnitPrice item = new UnitPrice();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UnitPriceId"))) item.UnitPriceId = (long)reader["UnitPriceId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUnitPriceFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateUnitPrice methods
            
        public virtual void CreateUnitPrice(UnitPrice unitPrice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, unitPrice.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, unitPrice.Summary);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, unitPrice.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, unitPrice.Enable);
                database.AddOutParameter(dbCommand, "@UnitPriceId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                unitPrice.UnitPriceId = (long)database.GetParameterValue(dbCommand, "@UnitPriceId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUnitPriceException, ex);
            }
        }

        #endregion

        #region UpdateUnitPrice methods
        
        public virtual void UpdateUnitPrice(UnitPrice unitPrice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceUpdate");            
                
                database.AddInParameter(dbCommand, "@UnitPriceId", DbType.Int64, unitPrice.UnitPriceId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, unitPrice.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, unitPrice.Summary);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, unitPrice.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, unitPrice.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateUnitPriceException, ex);
            }
        }
        
        #endregion

        #region DeleteUnitPrice methods
        public virtual void DeleteUnitPrice(long unitPriceId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceDelete");
                
                database.AddInParameter(dbCommand, "@UnitPriceId", DbType.Int64, unitPriceId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteUnitPriceException, ex);
            }
        }

        #endregion

        #region GetUnitPrice methods
        
        public virtual UnitPrice GetUnitPrice(long unitPriceId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceGet");
                
                database.AddInParameter(dbCommand, "@UnitPriceId", DbType.Int64, unitPriceId);
                
                UnitPrice unitPrice =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        unitPrice = CreateUnitPriceFromReader(reader);
                        reader.Close();
                    }
                }
                return unitPrice;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitPriceException, ex);
            }
        }
        
        #endregion

        #region GetUnitPriceList methods
        public virtual UnitPriceCollection GetUnitPriceList(UnitPriceColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UnitPriceCollection unitPriceCollection = new UnitPriceCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UnitPrice unitPrice = CreateUnitPriceFromReader(reader);
                        unitPriceCollection.Add(unitPrice);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return unitPriceCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitPriceListException, ex);
            }
        }
        
        public virtual UnitPriceCollection GetUnitPriceList(UnitPriceColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUnitPriceList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetUnitPriceList methods
        public virtual UnitPriceCollection GetUnitPriceListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitPriceGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UnitPriceCollection unitPriceCollection = new UnitPriceCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UnitPrice unitPrice = CreateUnitPriceFromReader(reader);
                        unitPriceCollection.Add(unitPrice);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return unitPriceCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitPriceListException, ex);
            }
        }
        
        public virtual UnitPriceCollection GetUnitPriceListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetUnitPriceListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}